<?php

namespace model;

use common\Model;
use common\Check;

class videos extends Model
{
	private static $table = "video";
	protected $DB;

	function __construct()
	{
		$this->DB = parent::db();
	}
	//获取栏目数据并分页
	public function jsonPageSelect()
	{
		//根据标题搜索内容
		if (!empty($_GET['title']))
			$where['a.title LIKE'] = '%' . $_GET['title'] . '%';

		//只搜索当前栏目内容
		if (isset($_GET['searchcid']))
			$newCid = $_GET['searchcid'] > 0 || $_GET['searchcid'] == 0 ? $_GET['searchcid'] : $_GET['cid'];
		else
			$newCid = $_GET['cid'];

		if ($newCid > 0)
			$where['a.cid'] = $newCid;

		//搜索 已审或草稿
		if (isset($_GET['stat']))
			$where['a.status'] = intval($_GET['stat']);

		$p = intval($_GET['page'] ?? 0) ?: 1;
		$num = intval($_GET['limit'] ?? 0) ?: 15;

		$page = ['num' => $num, 'p' => $p, 'return' => true];

		$field = 'b.id as cateid , b.name as catename,g.groupname,u.nickname ,a.id,a.cid,a.title,a.imgbl,a.att_a,a.att_b,a.att_c,a.status,a.time';
		$join[] = 'LEFT JOIN video_cate b ON a.cid=b.id';
		$join[] = 'LEFT JOIN admin_user u ON a.uid=u.id';
		$join[] = 'LEFT JOIN admin_group g ON u.gid=g.id';
		return [
			'data' => $this->DB->table(static::$table . ' a')->field($field)->where($where)->join($join)->Page($page)->order('a.id DESC')->select(),
			'page' => $this->DB->getPage()
		];
	}


	//关联查找一条数据
	public function findData()
	{
		$where['id'] = $_GET['id'];
		return  $this->DB->table(static::$table)->where($where)->find();
	}

	//添加数据
	public function insertData()
	{
		$data = [
			'cid' => $_POST['cid'],
			'title' => VarFilter($_POST['title']),
			'videourl' => VarFilter($_POST['videourl']),
			'att_a' => $_POST['att_a'],
			'att_b' => $_POST['att_b'],
			'att_c' => $_POST['att_c'],
			'examine' => VarFilter($_POST['examine']),
			'img' => $_POST['img'],
			'content' => $_POST['content'],
			'imgbl' => empty($_POST['img']) ? 0 : 1,
			'time' => date('Y-m-d H:i:s', time()),
			'status' => $_POST['status'],
			'uid' => LoginUserInfo("userid"),
			'gid' => LoginUserInfo("gid"),
		];
		//验证数据
		$msg = Check::rule(
			array(check::len($data['mast']), '标题必须为3-200个字，请检查'),
			array(check::len($data['title'], 6, 400), '标题必须为3-200个字，请检查'),
			array(check::isChinese($data['examine']), '审核人必须为中文，请检查'),
			array(check::isImg($data['img']), '缩略图必须为有效图片路径，请检查')
		);
		if ($msg !== true)
			return  ['status' => 0, 'msg' => $msg];

		$re_key = $this->DB->insert($data); //$this->error($this->DB->getError());

		if ($re_key > 0) {
			SqlLog("添加视频ID：" . $re_key); //写入日志
			return ['status' => 1, 'msg' => '添加成功', 'cid' => $_POST['cid']];
		} else {
			return ['status' => 0, 'msg' => '数据错误！添加失败'];
		}
	}
	//关联更新数据
	public function updateData()
	{
		$where['id'] = $_POST['id'];
		$data = [
			'cid' => $_POST['cid'],
			'title' => VarFilter($_POST['title']),
			'videourl' => VarFilter($_POST['videourl']),
			'att_a' => $_POST['att_a'],
			'att_b' => $_POST['att_b'],
			'att_c' => $_POST['att_c'],
			'examine' => VarFilter($_POST['examine']),
			'img' => $_POST['img'],
			'content' => $_POST['content'],
			'imgbl' => empty($_POST['img']) ? 0 : 1,
			'status' => $_POST['status'],
		];
		//验证数据
		$msg = Check::rule(
			array(check::len($_POST['mast']), '标题必须为3-200个字，请检查'),
			array(check::len($_POST['title'], 6, 400), '标题必须为3-200个字，请检查'),
			array(check::isChinese($_POST['examine']), '审核人必须为中文，请检查'),
			array(check::isImg($_POST['img']), '缩略图必须为有效图片路径，请检查')
		);
		if (!$msg)
			return ['status' => 0, 'msg' => $msg];

		$re_key = $this->DB->table(static::$table)->Where($where)->Update($data); //$this->error($this->DB->getError());

		if ($re_key > 0) {
			SqlLog("修改视频ID：" . $_POST['id']); //写入日志
			return ['key' => $re_key, 'status' => 1, 'msg' => '修改成功', 'cid' => $_POST['cid']];
		} else {
			return ['status' => 0, 'msg' => '数据没有变化！'];
		}
	}

	//删除一条数据
	public function deleteOneData()
	{
		$where['id'] = $_POST['id'];
		$find = $this->DB->table(static::$table)->where($where)->find();
		if ($find) {
			DelFile($find['videourl']); //删除文件
			DelFile($find['img']); //删除文件
			SqlLog("删除视频 ID：" . $_POST['id'] . " 路径：" . $find['videourl']); //写入日志
			return $this->DB->table(static::$table)->where($where)->Delete();
		} else {
			return false;
		}
	}

	//审核一条数据
	public function switchStatus()
	{
		$data = [$_POST['att'] => $_POST['status']];
		$up = $this->DB->table(static::$table)->where(['id' => $_POST['id']])->Update($data);
		if($up>0){
			SqlLog("设置视频属性ID：" . $_POST['id'] . " - " . $_POST['att']); //写入日志
			return  true;
		}else return  false;

		
		 
	}
}
